/*
 * COMP6471 Project1
 *  
 * This file is created by Yuan Tao (ewan.msn@gmail.com)
 * Licensed under GNU GPL v3
 * 
 * $Author: ewan.msn@gmail.com $
 * $Date: 2011-09-29 04:08:06 +0000 (Thu, 29 Sep 2011) $
 * $Rev: 19 $
 * $HeadURL: http://comp6471.googlecode.com/svn/Project1/src/util/logger.java $
 * 
 */

package util;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

import kklib.Cell;
import kklib.Layout;


class LoggerTextFormat extends Formatter { 
    @Override 
    public String format(LogRecord record) { 
    	return record.getLevel() + ": " + record.getMessage() + "\n"; 
    } 
}

class OutputTextFormat extends Formatter { 
    @Override 
    public String format(LogRecord record) { 
    	return record.getMessage() + "\n"; 
    } 
}

public class logger {
	public static Logger log = Logger.getLogger("Project1");
	public static Logger result = Logger.getLogger("Project1Result");
		
	public static void init() {
//		ConsoleHandler consoleHandle = new ConsoleHandler();
//		consoleHandle.setLevel(Level.ALL);
//		log.addHandler(consoleHandle);
		
		// add a file handler
		String path = System.getProperty("user.dir") + "\\log\\log.txt";
		FileHandler fileHandle;
		try {
			fileHandle = new FileHandler(path);
			fileHandle.setLevel(Level.SEVERE);
			fileHandle.setFormatter(new LoggerTextFormat());
			log.addHandler(fileHandle);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// create a logger to show the result of the program.
		path = System.getProperty("user.dir") + "\\output\\result.txt";
		try {
			fileHandle = new FileHandler(path);
			fileHandle.setLevel(Level.ALL);
			fileHandle.setFormatter(new OutputTextFormat());
			result.addHandler(fileHandle);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void info(String msg) {
		log.info(msg);
	}
	
	public static void err(String msg) {
		log.severe(msg);
	}
	
	public static void output(String msg) {
		result.info(msg);
		log.severe(msg);
	}
	
	public static void parserErr(String file, String msg) {
		String strMsg = "File: " + file + " " + "Syntax Error: " + msg; 
		result.info(strMsg);
		log.severe(strMsg);
	}

	public static void solutionErr(String file, String msg) {
		String strMsg = "File: " + file + " " + "Syntax Right. " + msg; 
		result.info(strMsg);
		log.severe(strMsg);
	}
	
	public static void solutionOK(String file, int cnt, int gameSize) {
		String strMsg = "File: " + file + " " 
			+ "Syntax Right. Number of possible solutions for (" + gameSize + "*" + gameSize + "): " 
			+ (cnt >= Layout.MAX_SOLUTIONS ? "At least " + Layout.MAX_SOLUTIONS : cnt); 
		result.info(strMsg);
		log.info(strMsg);
	}
	
	public static void printSolution(Layout layout, Cell c) {
		if (true) {		
			for (int i = 0; i < layout.gameSize; i++) {
				StringBuffer sb = new StringBuffer();
				for (int j = 0; j < layout.gameSize; j++) {
					sb.append(layout.rows.get(i*layout.gameSize + j).value + " ");
				}
				logger.info(sb.toString());
			}
			
			StringBuffer sb = new StringBuffer();
			sb.append(c.x + "," + c.y + ": ");
			for (int i = 0; i < c.possibleValues.size(); i++) {
				sb.append(c.possibleValues.get(i) + " ");
			}
			logger.info(sb.toString() + "\n");
		}
	}
}
